<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Groups_c extends CI_Controller {
        
        public function __construct() {
            parent::__construct();

            $this->load->model(array('groups_m', 'subscription_m', 'user_m'));
            $this->load->helper(array('form', 'html', 'url', 'array'));
            $this->load->library('form_validation');
        }

        
        public function index() {
            $data['body_id'] = 'admin_group';
            $user_id = $this->session->userdata('user_id');
            $data['role_id'] = $this->session->userdata('role_id');
            $data['rolename'] = $this->session->userdata('rolename');
            
            $data['title'] = 'Groups';
            $this->load->view('administrator/fragments/header_v', $data);
            
            
            if($data['role_id'] == 1 || $data['rolename'] == 'admin') {
                $data['groups'] = $this->groups_m->get_all_groups();

                for($i = 0; $i < count($data['groups']); $i++) {
                    $admins = $this->groups_m->getGroupAdmins($data['groups'][$i]['group_id']);

                    $data['groups'][$i]['admins'] = $admins;
                }
                $this->load->view('administrator/groups_v', $data);
            }
            else {
                $data['groups'] = $this->groups_m->get_groups_managed($user_id);

                for($i = 0; $i < count($data['groups']); $i++) {
                    $admins = $this->groups_m->getGroupAdmins($data['groups'][$i]['group_id']);

                    $data['groups'][$i]['admins'] = $admins;
                }
                $this->load->view('administrator/groups_v', $data);
            }
            
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        
        function search_group() {
            $data['body_id'] = 'admin_group';
            $data['title'] = "Find Group";
            if($_POST['txt_find'] == null || $_POST['txt_find'] == "") {
                redirect('groups');
            }
            else {
                $search_string = $_POST['txt_find'];
                $data['groups'] = $this->groups_m->get_like_groups($search_string);
                
                if($data['groups'] != null) {
                    $this->load->view('administrator/fragments/header_v', $data);
                    $user_id = $this->session->userdata('user_id');
                    $data['role_id'] = $this->session->userdata('role_id');
                    $data['rolename'] = $this->session->userdata('rolename');
                    
                    if($data['role_id'] == 2 || $data['rolename'] == 'author') {
                       $data['groups'] = $this->groups_m->get_groups_managed_like($user_id);
                    }
                    
                     for($i = 0; $i < count($data['groups']); $i++) {
                        $admins = $this->groups_m->getGroupAdmins($data['groups'][$i]['group_id']);
                        $data['groups'][$i]['admins'] = $admins;
                     }
                     $this->load->view('administrator/groups_v', $data);
                     $this->load->view('administrator/fragments/footer_v', $data);
                }
                
                else {
                    $this->load->view('administrator/fragments/header_v', $data);
                    $data['user_id'] = $this->session->userdata('user_id');
                    $data['role_id'] = $this->session->userdata('role_id');
                    $data['rolename'] = $this->session->userdata('rolename');
                    $data['groups'] = $this->groups_m->get_all_groups($data['user_id']);

                    $this->load->view('authenticated/no_group_v', $data);
                    $this->load->view('administrator/groups_v', $data);
                    $this->load->view('administrator/fragments/footer_v', $data);
                }
            }
        }

        
        function create_group() {
            $data['body_id'] = 'admin_group';
            $data['title'] = 'Create a new Group';
            $data['available_emps'] = $this->groups_m->get_all_employees();
            $data['all_departments'] = $this->user_m->get_all_departments();
            $data['isSubs'] = false;
            
            $error_message = "";
            $group_name_temp = "";
            $group_desc_temp = "";
            //$data['all_employees'] = $this->groups_m->get_all_employees();
            //$data['all_admins'] = $this->groups_m->get_admins_only();
            
            //$this->form_validation->set_rules('groupName', 'Group Name',
            //                                'required|xss_clean');
            //$this->form_validation->set_rules('groupDesc', 'Group Description',
            //                                'required|xss_clean');
            if(isset($_POST['btn_go'])) {
                $this->load->view('administrator/fragments/header_v', $data);
                
                 if($_POST['display_method_select'] == "All") {
                     $data['available_emps'] = $this->groups_m->get_all_employees();                     
                 }
                 else if($_POST['display_method_select'] == "Admins") {
                     $data['available_emps'] = $this->groups_m->get_admins_only();
                 }
                 else if($_POST['display_method_select'] == "Authors") {
                     $data['available_emps'] = $this->subscription_m->get_all_authors();
                 }
                 
                 $data['group_name_temp'] = "";
                 $data['group_desc_temp'] = "";
                 
                 if(!empty($_POST['groupName'])) {
                    $data['group_name_temp'] = $_POST['groupName'];
                 }
                 if(!empty($_POST['groupDesc'])) {
                    $data['group_desc_temp'] = $_POST['groupDesc'];
                 }
                 
                 if(!isset($_POST['isSubs']))
                    $data['isSubs'] = false;
                 else if(isset($_POST['isSubs']))
                    $data['isSubs'] = true;
                 
                 
                 if(!empty($_POST['available_emps'])) {
                     $data['admins'] = $_POST['available_emps'];
                 }
                 else if(empty($_POST['available_emps'])) {
                     $data['admins'] = array();
                 }
                 
                $data['error_message'] = $error_message;
                $this->load->view('administrator/create_group_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
            }
            
            else if (!isset($_POST['submit1']) ||
                    (isset($_POST['submit1']) && 
                        (empty($_POST['groupName']) || empty($_POST['groupDesc']) ||
                         empty($_POST['available_emps']))
                    )
                ) {
                
                $this->load->view('administrator/fragments/header_v', $data);
                
                if((isset($_POST['submit1']) && empty($_POST['groupName']))) {
                    $error_message .= "<p>GROUP NAME is required.</p>";
                    
                    if(!empty($_POST['groupDesc']))
                        $group_desc_temp = $_POST['groupDesc'];
                }
                if(isset($_POST['submit1']) && empty($_POST['groupDesc'])) {
                    $error_message .= "<p>GROUP DESCRIPTION is required.</p>";
                    
                    if(!empty($_POST['groupName']))
                        $group_name_temp = $_POST['groupName'];
                }
                if(isset($_POST['submit1']) && empty($_POST['available_emps'])) {
                    $error_message .= "<p>Choose at least ONE group admin.</p>";
                    
                    if(!empty($_POST['groupDesc']))
                        $group_desc_temp = $_POST['groupDesc'];
                    if(!empty($_POST['groupName']))
                        $group_name_temp = $_POST['groupName'];
                }
                
                if(!isset($_POST['isSubs']))
                    $data['isSubs'] = false;
                 else if(isset($_POST['isSubs']))
                    $data['isSubs'] = true;
                 
                 
                if(isset($_POST['submit1'])) {
                    if(!empty($_POST['available_emps'])) {
                        $data['admins'] = $_POST['available_emps'];
                    }
                    else if(empty($_POST['available_emps'])) {
                        $data['admins'] = array();
                    }
                }
                else {
                    $data['admins'] = array();
                }
                
                $data['error_message'] = $error_message;
                $data['group_desc_temp'] = $group_desc_temp;
                $data['group_name_temp'] = $group_name_temp;
                
                $this->load->view('administrator/create_group_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
            }
            else if(isset($_POST['submit1']) &&
                    (!empty($_POST['groupName']) && !empty($_POST['groupDesc'])
                    && !empty($_POST['available_emps']))) {
                $selected_emps = $_POST['available_emps'];
                $can_subscribe;
                $password;

                if(isset($_POST['isSubs'])) {
                    $can_subscribe = 1;
                    
                    $password = $_POST['group_password'];
                    if($password == null) $password = '';
                }
                 else
                {
                   $can_subscribe = 0;
                   $password = '';
                }
                
                $this->groups_m->insert_group($selected_emps, $can_subscribe, $password);
                redirect('groups');
            }
        }
        
        

        function edit_group($group_id) {
            $this->load->library('encrypt');
            
            $data['body_id'] = 'admin_group';
            $group = $this->groups_m->get_group($group_id);
            $data['title'] = 'Edit Group Details';
            $data['role_id'] = $this->session->userdata('role_id');
            $data['rolename'] = $this->session->userdata('rolename');
            $error_message = "";
            $data['password'] = $group['password'];
            
            $data['group_name'] = "";
            $data['group_desc'] = "";
            $data['groups'] = $group;

            $this->form_validation->set_rules('groupName', 'Group Name',
                                            'required|xss_clean');
            $this->form_validation->set_rules('groupDesc', 'Group Description',
                                            'required|xss_clean');
             $this->load->view('administrator/fragments/header_v', $data);
             
            if(isset($_POST['btn_go'])) {
                if(!empty($_POST['groupName'])) {
                    $data['group_name'] = $_POST['groupName'];
                 }
                 if(!empty($_POST['groupDesc'])) {
                    $data['group_desc'] = $_POST['groupDesc'];
                 }
                 
                 if(!isset($_POST['isSubs'])) {
                    $data['isSubs'] = false;
                 }
                 else if(isset($_POST['isSubs']))
                    $data['isSubs'] = true;
                 
                 if($_POST['display_method_select'] == "All") {
                     $data['available_emps'] = $this->groups_m->get_all_employees();                     
                 }
                 else if($_POST['display_method_select'] == "Admins") {
                     $data['available_emps'] = $this->groups_m->get_admins_only();
                 }
                 else if($_POST['display_method_select'] == "Authors") {
                     $data['available_emps'] = $this->subscription_m->get_all_authors();
                 }
                 $data['admins'] = array();
                 
                 if(!empty($_POST['available_emps'])) {
                    $selected_emps = $_POST['available_emps'];
                    
                    if($selected_emps != NULL) {
                       for($i = 0; $i < count($selected_emps); $i++) {
                           $admins[$i] = $selected_emps[$i];
                       }
                       $data['admins'] = $admins;
                    }
                 }
                 
                $data['error_message'] = $error_message;
                $this->load->view('administrator/edit_group_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
            }
            else if (isset($_POST['submit2']) &&
                    (!empty($_POST['groupName']) && !empty($_POST['groupDesc'])
                    && !empty($_POST['available_emps']))) {
                $data = array(
                            'group_name' => $this->input->post('groupName'),
                            'group_desc' => $this->input->post('groupDesc')
                        );
                
                    if ($this->form_validation->run() === TRUE) {
                        $selected_emps = $_POST['available_emps'];
                        
                        if(isset($_POST['isSubs'])) {
                            $data['is_subscribable'] = 1;
                            $data['password'] = $_POST['group_password'];
                            if($data['password'] == null) $data['password'] = '';
                        }
                        else if(!isset($_POST['isSubs'])) {
                            $data['is_subscribable'] = 0;
                            $data['password'] = "";
                        }

                        $this->groups_m->update_group($group_id, $selected_emps, $data);
                        redirect('groups');
                    }
            }
            else if (!isset($_POST['submit2']) ||
                    (isset($_POST['submit2']) && 
                        (empty($_POST['groupName']) || empty($_POST['groupDesc']) ||
                         empty($_POST['available_emps']))
                    )
                ) {
                if(!isset($_POST['submit2'])) {
                    if($group['is_subscribable'] == 1) {
                        $data['isSubs'] = true;
                    }
                    else if($group['is_subscribable'] == 0) {
                        $data['isSubs'] = false;
                    }
                    $data['group_name'] = $group['group_name'];
                    $data['group_desc'] = $group['group_desc'];
                }
                else if(isset($_POST['submit2'])) {
                    if(!isset($_POST['isSubs']))
                        $data['isSubs'] = false;
                    else if(isset($_POST['isSubs']))
                        $data['isSubs'] = true;
                }
                
                //validation
                if(isset($_POST['submit2']) && empty($_POST['groupName'])) {
                    $error_message .= "<p>GROUP NAME is required.</p>";
                    
                    if(!empty($_POST['groupDesc'])) {
                        $data['group_desc'] = $_POST['groupDesc'];
                    }
                }
                if(isset($_POST['submit2']) && empty($_POST['groupDesc'])) {
                    $error_message .= "<p>GROUP DESCRIPTION is required.</p>";
                    
                    if(!empty($_POST['groupName'])) {
                        $data['group_name'] = $_POST['groupName'];
                    }
                }
                if(isset($_POST['submit2']) && empty($_POST['available_emps'])) {
                    $error_message .= "<p>Choose at least ONE group admin.</p>";
                    
                    if(!empty($_POST['groupDesc']))
                        $data['group_desc'] = $_POST['groupDesc'];
                    if(!empty($_POST['groupName']))
                        $data['group_name'] = $_POST['groupName'];
                }
                
                $data['available_emps'] = $this->groups_m->get_all_employees();
                
                if(!isset($_POST['submit2'])) {
                    $ads = $this->groups_m->getGroupAdmins($group_id);
                    if($ads != NULL) {
                        for($i = 0; $i < count($ads); $i++) {
                            $admins[$i] = $ads[$i]['user_id'];
                        }
                        $data['admins'] = $admins;
                    }
                }
                else if(isset($_POST['submit2'])) {
                    if(!empty($_POST['available_emps'])) {
                        $data['admins'] = $_POST['available_emps'];
                    }
                    else if(empty($_POST['available_emps'])) {
                        $data['admins'] = array();
                    }
                }
                
                $data['error_message'] = $error_message;
                
                $this->load->view('administrator/edit_group_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
            }
        }


        function delete_group($group_id) {
            $data['body_id'] = 'admin_group';
            $data['group_to_delete'] = $this->groups_m->get_group($group_id);
            $data['title'] = "Delete Group";
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/sure_delete_v', $data );
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        
        function sure_delete($group_id) {
            $data['body_id'] = 'admin_group';
            $this->load->model('groups_m');
            $this->groups_m->delete_group($group_id);
            redirect('groups');
            
            
        }
        
        function manage($group_id) {
            $data['body_id'] = 'admin_group';
            $data['title'] = 'Manage Group';
            $data['group_id'] = $group_id;
            
            if(!isset($_POST['btnSaveSubs']) && !isset($_POST['btn_filter_available'])) {
                $data['checked_all_users'] = true;
                $data['checked_admins'] = true;
                $data['checked_employees'] = true;
                $data['checked_students'] = true;
                    
                $data['subscribers'] = $this->subscription_m->get_subscribers($group_id); //call the subscription model
                $data['all_available'] = $this->subscription_m->get_all_unsubscribed_students($group_id);

                $this->load->view('administrator/fragments/header_v', $data);
                $this->load->view('administrator/manage_group_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
            }
            else if(isset($_POST['btnSaveSubs'])) {
                //echo "Hello";
                $selected_subs = $_POST['subscribers'];
                //print_r($selected_subs);
                $this->subscription_m->insert_subscriber($group_id, $selected_subs, 1);
                redirect('groups');
            }
            else if(isset($_POST['btn_filter_available'])) {
                if(isset($_POST['chk_all_users']) ||
                        (!isset($_POST['chk_site_admins']) && !isset($_POST['chk_employees'])
                        && !isset($_POST['chk_students']) )) {
                    //echo "All users";
                    $data['checked_all_users'] = true;
                    $data['checked_admins'] = true;
                    $data['checked_employees'] = true;
                    $data['checked_students'] = true;
                   
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_all_not_subscribed($group_id);
                        $real_available = array();

                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_all_not_subscribed($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
                else if(isset($_POST['chk_site_admins']) && isset($_POST['chk_employees'])) {
                    //echo "Site Admins and Employees";
                    $data['checked_all_users'] = false;
                    $data['checked_admins'] = true;
                    $data['checked_employees'] = true;
                    $data['checked_students'] = false;
                    
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_admins_and_employees($group_id);
                        $real_available = array();
                        
                        foreach($available_temp as $av) {
                            if(in_array($av, $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[count($real_available) + 1] = $av;
                            }
                        }
                        
                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_admins_and_employees($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
                else if(isset($_POST['chk_site_admins']) && isset($_POST['chk_students'])) {
                    //echo "Site Admins and Students";
                    
                    $data['checked_all_users'] = false;
                    $data['checked_admins'] = true;
                    $data['checked_employees'] = false;
                    $data['checked_students'] = true;
                    
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_admin_and_students($group_id);
                        $real_available = array();

                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_admin_and_students($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
                else if(isset($_POST['chk_employees']) && isset($_POST['chk_students'])) {
                    //echo "Employees and Students";
                    
                    $data['checked_all_users'] = false;
                    $data['checked_admins'] = false;
                    $data['checked_employees'] = true;
                    $data['checked_students'] = true;
                    
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_employees_and_students($group_id);
                        $real_available = array();

                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_employees_and_students($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
                else if(isset($_POST['chk_site_admins'])) {
                    //echo "Site Admins";
                    $data['checked_all_users'] = false;
                    $data['checked_admins'] = true;
                    $data['checked_employees'] = false;
                    $data['checked_students'] = false;
                    
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_all_admins($group_id);
                        $real_available = array();

                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_all_admins($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
                else if(isset($_POST['chk_employees'])) {
                    //echo "Employees";
                    $data['checked_all_users'] = false;
                    $data['checked_admins'] = false;
                    $data['checked_employees'] = true;
                    $data['checked_students'] = false;
                    
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_all_employees_only($group_id);
                        $real_available = array();

                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_all_employees_only($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
                else if(isset($_POST['chk_students'])) {
                    //echo "Students";
                    $data['checked_all_users'] = false;
                    $data['checked_admins'] = false;
                    $data['checked_employees'] = false;
                    $data['checked_students'] = true;
                    
                    if(!empty($_POST['subscribers'])) {
                        $subs_user_ids = $_POST['subscribers'];
                        $subs_names = array();

                        for($i = 0; $i < count($subs_user_ids); $i++) {
                            $user_name = $this->subscription_m->get_subscriber_name($subs_user_ids[$i]);
                            $subs_names[$i] = $user_name;
                        }

                        $data['subscribers'] = $subs_names;

                        $available_temp = $this->subscription_m->get_all_students_only($group_id);
                        $real_available = array();

                        for($i = 0; $i < count($available_temp); $i++) {
                            if(in_array($available_temp[$i], $subs_names)) {
                                 continue;
                            }
                            else {
                                $real_available[$i] = $available_temp[$i];
                            }
                        }

                        $data['all_available'] = $real_available;
                    }
                    else {
                        $data['all_available'] = $this->subscription_m->get_all_students_only($group_id);
                    }
                    $this->load->view('administrator/fragments/header_v', $data);
                    $this->load->view('administrator/manage_group_v', $data);
                }
            }
        }


        function success_subscribe() {
            $data['body_id'] = 'admin_group';
            $user_id = 6;   //TEMPORARY!!!

            $this->load->model('subscription_m');
            $subs_groups = $this->groups_m->get_all_subscribable_groups();
            $final_groups = array();

            for($i = 0; $i < count($subs_groups); $i++) {
                $group_id = $subs_groups[$i]['group_id'];
                $data = array(
                            'user_id' => $user_id,
                            'group_id' => $group_id
                        );

                if($this->subscription_m->is_user_subscribed_to_group($data) == false) {
                    $final_groups[$i] = $subs_groups[$i];
                }
            }

            $data['groups'] = $final_groups;
            $data['title'] = 'Groups';
            $data['heading'] = "You have successfully subscribed to the group!";
            $this->load->view('studentViewGroups', $data);
        }
        
    }
?>
